package com.pinyougou.sellergoods.service.impl;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.common.json.JSON;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbBrandMapper;
import com.pinyougou.mapper.TbGoodsDescMapper;
import com.pinyougou.mapper.TbGoodsMapper;
import com.pinyougou.mapper.TbItemCatMapper;
import com.pinyougou.mapper.TbItemMapper;
import com.pinyougou.mapper.TbSellerMapper;
import com.pinyougou.mapper.TbTypeTemplateMapper;
import com.pinyougou.pojo.TbBrand;
import com.pinyougou.pojo.TbGoods;
import com.pinyougou.pojo.TbGoodsDesc;
import com.pinyougou.pojo.TbGoodsExample;
import com.pinyougou.pojo.TbGoodsExample.Criteria;
import com.pinyougou.pojo.TbItem;
import com.pinyougou.pojo.TbItemCat;
import com.pinyougou.pojo.TbItemExample;
import com.pinyougou.pojo.TbSeller;
import com.pinyougou.sellergoods.service.GoodsService;
import com.pinyougou.sellergoods.service.itemCatService;
import com.pinyougou.untis.Goods;
import com.pinyougou.untis.PageResult;
@Service
@Transactional
public class GoodsServiceImpl implements GoodsService{
	@Autowired
	private TbGoodsMapper goodsMapper;
	@Autowired
	private TbGoodsDescMapper goodsDescMapper;
	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbTypeTemplateMapper templateMapper;
	@Autowired
	private TbBrandMapper brandMapper;
	@Autowired
	private TbItemCatMapper itemCatMapper;
	@Autowired
	private TbSellerMapper sellerMapper;
	@Override
	/**
	 * 添加方法
	 */
	public void add(Goods goods) {
		
		goods.getGoods().setAuditStatus("0");//设置状态码
		System.out.println(goods.getGoods().getId());
		goodsMapper.insert(goods.getGoods());
		goods.getGoodsDesc().setGoodsId(goods.getGoods().getId());//设置GoodsDesc的id
		
		goodsDescMapper.insert(goods.getGoodsDesc());
		
		publicSave(goods);
		
		
		
		
	}
	/**
	 * 如果没点启用规格，默认添加一些规格
	 * @param goods
	 * @param item
	 */
	
	public void set(Goods goods,TbItem item) {
		
			item.setGoodsId(goods.getGoods().getId());
			item.setSellerId(goods.getGoods().getSellerId());
			item.setCategoryid(goods.getGoods().getCategory1Id());
			item.setCreateTime(new Date());
			item.setUpdateTime(new Date());
			
			TbBrand brand = brandMapper.selectByPrimaryKey(goods.getGoods().getBrandId());
			//品牌名称
			item.setBrand(brand.getName());
			
			TbItemCat itemcat = itemCatMapper.selectByPrimaryKey(goods.getGoods().getCategory3Id());
			item.setCategory(itemcat.getName());
			
			//商家名称
			TbSeller seller = sellerMapper.selectByPrimaryKey(goods.getGoods().getSellerId());
			
			item.setSeller(seller.getNickName());
			
			List<Map> array = com.alibaba.fastjson.JSON.parseArray(goods.getGoodsDesc().getItemImages(), Map.class);
			System.out.println(goods.getGoodsDesc().getItemImages());
			//去处集合中的每一个图片
			
				if(array.size()>0) {
					item.setImage((String)array.get(0).get("url"));
				}
				
				itemMapper.insert(item);
			}

	@Override
	public PageResult search(TbGoods goods, int page, int size) {
		PageHelper.startPage(page, size);
		
		TbGoodsExample example =new TbGoodsExample();
		Criteria criteria = example.createCriteria();
		
		criteria.andIsDeleteIsNull();//查询出IsDelete这个字段是Null的值
		
		if(goods!=null) {
			if(goods.getSellerId()!=null &&goods.getSellerId().length()>0 ) {
				criteria.andSellerIdEqualTo(goods.getSellerId());
//				criteria.andSellerIdLike("%"+goods.getSellerId()+"%");
			}
			if(goods.getGoodsName()!=null && goods.getGoodsName().length()>0){
				criteria.andGoodsNameLike("%"+goods.getGoodsName()+"%");
			}
			if(goods.getAuditStatus()!=null && goods.getAuditStatus().length()>0){
				criteria.andAuditStatusLike("%"+goods.getAuditStatus()+"%");
			}
			if(goods.getIsMarketable()!=null && goods.getIsMarketable().length()>0){
				criteria.andIsMarketableLike("%"+goods.getIsMarketable()+"%");
			}
			if(goods.getCaption()!=null && goods.getCaption().length()>0){
				criteria.andCaptionLike("%"+goods.getCaption()+"%");
			}
			if(goods.getSmallPic()!=null && goods.getSmallPic().length()>0){
				criteria.andSmallPicLike("%"+goods.getSmallPic()+"%");
			}
			if(goods.getIsEnableSpec()!=null && goods.getIsEnableSpec().length()>0){
				criteria.andIsEnableSpecLike("%"+goods.getIsEnableSpec()+"%");
			}
			if(goods.getIsDelete()!=null && goods.getIsDelete().length()>0){
				criteria.andIsDeleteLike("%"+goods.getIsDelete()+"%");
			}
		}
		
		
		Page<TbGoods> page2 =(Page<TbGoods>) goodsMapper.selectByExample(example);
		
		
		
		
		return new PageResult(page2.getTotal(), page2.getResult());
	}
	/**
	 * 根据id返回组合实体类
	 */
	@Override
	public Goods findOne(Long id) {
		Goods goods=new Goods();
		TbGoods tbGoods = goodsMapper.selectByPrimaryKey(id);
		goods.setGoods(tbGoods);
		
		TbGoodsDesc tbGoodsDesc = goodsDescMapper.selectByPrimaryKey(id);
		goods.setGoodsDesc(tbGoodsDesc);
		
		TbItemExample example =new TbItemExample();
		com.pinyougou.pojo.TbItemExample.Criteria criteria = example.createCriteria();
		criteria.andGoodsIdEqualTo(id);
		List<TbItem> list = itemMapper.selectByExample(example);
		goods.setItem(list);
		return goods;
	}
	
	/**
	 * 修改的公共类添加
	 */
	public void publicSave(Goods goods) {
		if ("1".equals(goods.getGoods().getIsEnableSpec())) {
			for(TbItem item :goods.getItem()) {
				//给item添加标题
				String title = goods.getGoods().getGoodsName();
				//添加spec	①问题
				Map<String,Object> spec= com.alibaba.fastjson.JSON.parseObject(item.getSpec());
				for(String key : spec.keySet()) {
					title+= " "+spec.get(key);
					
				}
				
				item.setTitle(title);
				
				set(goods,item);
				itemMapper.insert(item);
			}
				
		} else {
			TbItem item=new TbItem();
			item.setTitle(goods.getGoods().getGoodsName());//商品 KPU+规格描述串作为
			//SKU 名称
			item.setPrice( goods.getGoods().getPrice() );//价格
			item.setStatus("1");//状态
			item.setIsDefault("1");//是否默认
			item.setNum(99999);//库存数量
			item.setSpec("{}");
			
			set(goods,item);
			itemMapper.insert(item);
		}
	}
	
	/**
	 * 修改商家
	 */
	public void update(Goods goods) {

		//设置未申请状态:如果是经过修改的商品，需要重新设置状态
		goods.getGoods().setAuditStatus("0");
		
		
		goodsMapper.updateByPrimaryKey(goods.getGoods());
		
		goodsDescMapper.updateByPrimaryKey(goods.getGoodsDesc());
		
		
		
		TbItemExample example =new TbItemExample();
		com.pinyougou.pojo.TbItemExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andGoodsIdEqualTo(goods.getGoods().getId());
		//先删除SKU列表在保存SKU列表
		itemMapper.deleteByExample(example);
		
		
		//添加新的SKU列表
		publicSave(goods);
		
	}
	/**
	 * 修改状态码
	 */
	
	@Override
	public void updateStatus(Long[] ids, String status) {
			for(Long id:ids) {
				TbGoods key = goodsMapper.selectByPrimaryKey(id);
				key.setAuditStatus(status);
				goodsMapper.updateByPrimaryKey(key);
				
			}

		
	}
	/**
	 * 逻辑删除
	 */
	@Override
	public void dele(Long[] ids) {
		for(Long id :ids) {
			TbGoods goods = goodsMapper.selectByPrimaryKey(id);
			goods.setIsDelete("1");
			goodsMapper.updateByPrimaryKey(goods);
		}
		
		
	}
	@Override
	public List<TbItem> findItemListByGoodsIdandStatus(Long[] goodsIds, String status) {
		
		
		TbItemExample example =new TbItemExample();
		com.pinyougou.pojo.TbItemExample.Criteria criteria = example.createCriteria();
		
		criteria.andGoodsIdIn(Arrays.asList(goodsIds));
		criteria.andStatusEqualTo(status);
		
		
		return itemMapper.selectByExample(example);
	}
	

}